package com.glsc.ngateway.flowable.service;

import cn.hutool.core.collection.CollUtil;
import com.glsc.ngateway.common.base.domain.mysql.flowable.ActHiComment;
import com.glsc.ngateway.common.base.domain.mysql.flowable.ActHiTaskinst;
import com.glsc.ngateway.common.base.repo.mysql.flowable.ActHiCommentRepo;
import com.glsc.ngateway.common.base.repo.mysql.flowable.FlowableCommonRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FlowableCommonService {

    private static final Logger logger = LoggerFactory.getLogger(FlowableCommonService.class);

    @Autowired
    FlowableCommonRepo flowableCommonRepo;

    @Autowired
    private ActHiCommentRepo actHiCommentRepo;

    public Page<List<Map<String, String>>> findUserDone(String involveUser, String processDefinitionKeyStr, List<String> processDefinitionKey, String title, Date begin, Date end, String sysid, String hasEndForInvolve, List<String> processIdList, Pageable pageable) {
        return findUserDone(involveUser, processDefinitionKeyStr, processDefinitionKey, title, begin, end, sysid, hasEndForInvolve, processIdList, null, pageable);
    }
    public Page<List<Map<String, String>>> findUserDone(String involveUser, String processDefinitionKeyStr, List<String> processDefinitionKey, String title, Date begin, Date end, String sysid, String hasEndForInvolve, List<String> processIdList, List<String> processIdNotList, Pageable pageable) {
        String processIdListStr = null;
        if (CollUtil.isNotEmpty(processIdList)) {
            processIdListStr = CollUtil.join(processIdList, ",");
        }
        String processIdNotListStr = null;
        if (CollUtil.isNotEmpty(processIdList)) {
            processIdNotListStr = CollUtil.join(processIdNotList, ",");
        }
        return flowableCommonRepo.findUserDone(involveUser, processDefinitionKeyStr, processDefinitionKey, title, begin, end, sysid, hasEndForInvolve, processIdList, processIdListStr, processIdNotList, processIdNotListStr, pageable);
    }

    public List<Map<String, String>> findUserParticipateProcessList(List<String> processIdList, String user, String sysId) {
        return flowableCommonRepo.findUserParticipateProcessList(processIdList, user, sysId);
    }

    /**
     * 添加新的历史节点
     */
    public void addNewHiTask(String processDefId, String processInstId, String assList, String taskId, String taskName, String tenantId){
        ActHiTaskinst hiTaskinst = new ActHiTaskinst();
        hiTaskinst.setId(taskId);
        hiTaskinst.setProcessDefinitionId(processDefId);
        hiTaskinst.setProcessInstanceId(processInstId);
        hiTaskinst.setTitle(taskName);
        hiTaskinst.setAssignee(assList);
        hiTaskinst.setStartTime(LocalDateTime.now());
        hiTaskinst.setEndTime(LocalDateTime.now());
        hiTaskinst.setTenantId(tenantId);

        flowableCommonRepo.save(hiTaskinst);
    }

    public void addComment(ActHiComment comment) {
        actHiCommentRepo.save(comment);
    }

    /**
     * 更改已经结束流程的标题
     */
    public int updateProcessTitle(String processId, String title) {
        return flowableCommonRepo.updateHistoryEndProcessTitle(processId, title);
    }
}